<?php
/**
 * signPush.php 打卡提示推送 每天8:00推送
 *
 * @author 周剑锋 <camfee@foxmail.com>
 * @date   17-9-6 上午10:16
 *
 */

require_once dirname(__DIR__) . "/cronBase.php";

use Common\Bridge;
use Center\User as CUser;
use Mobile\AppPush;
use Feed\FeedTips;
use Constants\SchemeConstant;
use Mobile\RecomData;

class signPush extends cronBase
{
    const DATA_LIMIT = 200;

    public function doDefault()
    {
        //        $pdo = Bridge::pdo(Bridge::DB_USER_R);
        //        //取用户数据
        //        $count = $pdo->select("max(UserId)")->from("User")->where(["UserType >" => CUser::USER_TYPE_ENROLL])->getValue();
        //        if ($count > 0) {
        //            $cnt = ceil($count / self::DATA_LIMIT);
        //            for ($i = 0; $i < $cnt; $i++) {
        //                if (empty($pdo)) {
        //                    $pdo = Bridge::pdo(Bridge::DB_USER_R);
        //                }
        //                $res = $pdo->clear()->select("UserId,UserType")->from("User")->where([
        //                    "UserId >=" => $i * self::DATA_LIMIT,
        //                    "UserId <" => ($i + 1) * self::DATA_LIMIT,
        //                ])->getAll();
        //                if (!empty($res) && count($res) > 0) {
        //                    foreach ($res as $k => $v) {
        //                        self::_sendPushFeed($v);
        //                    }
        //                }
        //                $pdo->close();
        //                Bridge::pdo(Bridge::DB_USER_R, 'force_close');
        //                $pdo = null;
        //            }
        //        }
        self::_sendPush(); // 推送全部用户
    }

    private static function _sendPushFeed($info)
    {
        if (!empty($info['UserId'])) { // 打卡feed
            if ($info['UserType'] == CUser::USER_TYPE_ENROLL) {
                $msg = '今天还未打卡称重，站在秤上立即打卡';
            } else {
                $msg = '今天还未称重，站在秤上立即称重';
            }
            $now = time();
            FeedTips::instance($info['UserId'], 0)->add(FeedTips::FEED_TYPE_UN_SIGN, $now,
                SchemeConstant::APP_MEITE_INDEX, $msg);
        }
    }

    private static function _sendPush()
    {
        $msg = (string)RecomData::getData(RecomData::APP_SIGN_PUSH_MSG)[RecomData::APP_SIGN_PUSH_MSG]['Msg'];
        if (!empty($msg)) {
            AppPush::pushAll(AppPush::PUSH_TYPE_MSG, $msg);
        }
    }
}

$app->run();